﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");

		response = structNew();
		structInsert(response, "success", false, true);

		taskId = event.getArg("tsk");
		studentId = event.getArg("stu");
		
		transaction {
			
			/* 载入教学任务 */
			sql = "SELECT 1 FROM t_new_examAdvice_student where task_id = :taskId and stu_id = :studentId";
			
			queryObj = new Query(datasource=application.dnsMaster);
			
			queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");
			
			rs_examtaskStudent = queryObj.execute( sql=sql ).getResult();
			
			
			sql = "SELECT cr_id, date_exam, time_start, time_end FROM t_new_examTaskAdvice where task_id = :taskId ";
			
			queryObj = new Query(datasource=application.dnsMaster);
			
			queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			
			rs_examtask= queryObj.execute( sql=sql ).getResult();
			
			if( rs_examtaskStudent.recordCount ){
				
				sql = "DELETE FROM t_new_examAdvice_student where task_id = :taskId and stu_id = :studentId";
				
				queryObj = new Query(datasource=application.dnsMaster);
				queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");
				
				queryObj.execute( sql=sql );
				
				
				sql = "SELECT 1 FROM t_new_examAdvice_student where task_id = :taskId and stu_id = :studentId";
			
				queryObj = new Query(datasource=application.dnsMaster);
				
				queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");
				
				rs_examtaskStudent_t = queryObj.execute( sql=sql ).getResult();
				
				if( rs_examtaskStudent_t.recordCount eq 0){
					
					if ( rs_examtask.cr_id eq "" && rs_examtask.cr_id eq "" && rs_examtask.cr_id eq "" && rs_examtask.cr_id eq ""){
						
						sql = "DELETE FROM t_new_examtaskAdvice where task_id = :taskId ";
				
						queryObj = new Query(datasource=application.dnsMaster);
						queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
						
						queryObj.execute( sql=sql );
					}
				}
				
			}else{
				
				sql = "INSERT into t_new_examAdvice_student(stu_id, task_id) values(:studentId, :taskId)";	
				
				queryObj = new Query(datasource=application.dnsMaster);
				queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");
				
				queryObj.execute( sql=sql );
				
				if( rs_examtask.recordCount eq 0){
					
					sql = "INSERT INTO t_new_examtaskAdvice(task_id, is_arranged)values( :taskId ,'2') ";		
					
					queryObj = new Query(datasource=application.dnsMaster);
					queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
					
					queryObj.execute( sql=sql );
					
				}else{
					sql = "UPDATE t_new_examTaskAdvice SET is_arranged = '2' where task_id = :taskId";	
					
					queryObj = new Query(datasource=application.dnsMaster);
					queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
					
					queryObj.execute( sql=sql );
				}
				
			}
			
		
			
			structInsert(response, "success", true, true);
			
		}
		
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>